package com.example.service.impl;

import com.example.mapper.controlPlanNewMapp;
import com.example.pojo.controlPlanNewPojo;
import com.example.pojo.controlPlanPojo;
import com.example.service.controlPlanNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName:ddmControlPlanImpl
 * Description:
 *
 * @Author 赵永峰
 * @Create 2025/6/30 15:35
 * @Version 1.0
 */
@Service
public class controlPlanNewImpl implements controlPlanNewService {
    @Autowired
    private controlPlanNewMapp controlPlanNewMapp;
    //对控制计划插入多行数据
    public void insertMultiLine(List<controlPlanNewPojo> pojos){
        //对修改时间、创建时间进行赋值
        for (int i = 0; i < pojos.size(); i++) {
            pojos.get(i).setCreate_time(LocalDateTime.now());
            pojos.get(i).setUpdate_time(LocalDateTime.now());
        }
        controlPlanNewMapp.insertMultiLine(pojos);
    }
    //辅助函数：根据两个字符串类型的字段，查询分别等于某一值的数据(控制计划的表）
    public List<controlPlanNewPojo> queryTwoField(Map<String, Object> map){
        String firstFieldName=(String) map.get("firstFieldName");
        String firstFieldVal=(String) map.get("firstFieldVal");
        String secondFieldName=(String) map.get("secondFieldName");
        String secondFieldVal=(String) map.get("secondFieldVal");
        List<controlPlanNewPojo> pojos= controlPlanNewMapp.queryTwoField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal);
        return pojos;
    }


    //根据ID，修改一行的值
    public void editOneById(controlPlanNewPojo pojo){
        //定义修改时间
        pojo.setUpdate_time(LocalDateTime.now());
        //调Mapper层
        controlPlanNewMapp.editOneById(pojo);
    }


    //根据三个字段查询所有数据：现在用在了根据工序号、发动机图号、车间名称进行查询。利用的是模糊查询。
    public List<controlPlanNewPojo> queryThreeField(Map<String, Object> parameters){
        String firstFieldName=(String) parameters.get("firstFieldName");
        String firstFieldVal=(String) parameters.get("firstFieldVal");
        String secondFieldName=(String) parameters.get("secondFieldName");
        String secondFieldVal=(String) parameters.get("secondFieldVal");
        String thirdFieldName=(String) parameters.get("thirdFieldName");
        String thirdFieldVal=(String) parameters.get("thirdFieldVal");
        List<controlPlanNewPojo> pojoList=controlPlanNewMapp.queryThreeField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal,thirdFieldName,thirdFieldVal);
        return pojoList;
    }

    //根据id查询一行
    public List<controlPlanNewPojo> queryOneById(String id){
        List<controlPlanNewPojo> pojos=controlPlanNewMapp.queryOneById(id);
        return pojos;
    }

    //插入一行，向上或向下插入
    public void insertOne(controlPlanNewPojo controlPlanNewPojo) {
        //生成一段低代码格式的id
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        controlPlanNewPojo.setId(uuidStr);
        //定义创建时间生成规则
        controlPlanNewPojo.setCreate_time(LocalDateTime.now());
        //定义修改时间生成规则
        controlPlanNewPojo.setUpdate_time(LocalDateTime.now());
        controlPlanNewMapp.insertOne(controlPlanNewPojo);
    }
    public List<controlPlanNewPojo> inquiryControlPlanAssociationByOperateGuideBook(String param) {
        String[] fieldIds = param.split("\\|\\*\\|");
        List<controlPlanNewPojo> resultList = new ArrayList<>();

        for (String fieldId : fieldIds) {
            try {
                List<controlPlanNewPojo> pojos = controlPlanNewMapp.queryOneById(fieldId);
                if (pojos != null) {
                    resultList.addAll(pojos); // 使用 addAll 合并列表
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        return resultList;
    }
}
